package com.example.demo.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

/**
* @author renshuai
* 
* 控制台输入相应模块以及实体，Controller，service等名称，自动生成代码
*/
public class MpGenerator {

   /**
    * <p>
    * 读取控制台内容
    * </p>
    */
   public static String scanner(String tip) {
       Scanner scanner = new Scanner(System.in);
       StringBuilder help = new StringBuilder();
       help.append("帅哥，请输入一下你的" + tip + "：");
       System.out.println(help.toString());
       if (scanner.hasNext()) {
           String ipt = scanner.next();
           if (StringUtils.isNotEmpty(ipt)) {
               return ipt;
           }
       }
       throw new MybatisPlusException("请输入正确的" + tip + "！");
   }

   public static void main(String[] args) {
       // 代码生成器
       AutoGenerator mpg = new AutoGenerator();

       // 全局配置
       GlobalConfig gc = new GlobalConfig();
       String projectPath = System.getProperty("user.dir");
       gc.setOutputDir(projectPath + "/src/main/java");
       gc.setAuthor("renshuai");
       gc.setOpen(false);
       gc.setSwagger2(true);
       gc.setBaseResultMap(true);
       gc.setIdType(IdType.AUTO);
       //自定义文件名
       gc.setEntityName(scanner("实体类名"));
       gc.setControllerName(scanner("Controlller名"));
       gc.setServiceName(scanner("Service名"));
       gc.setServiceImplName(scanner("ServiceImpl名"));
       gc.setMapperName(scanner("Mapper名"));
       gc.setXmlName(scanner("XML名"));
       mpg.setGlobalConfig(gc);

       // 数据源配置
       DataSourceConfig dsc = new DataSourceConfig();
       dsc.setUrl("jdbc:mysql://localhost:3306/linjiashop?serverTimezone=Hongkong&useSSL=false&useUnicode=true&characterEncoding=UTF-8");
       dsc.setDriverName("com.mysql.cj.jdbc.Driver");
       dsc.setUsername("root");
       dsc.setPassword("123456");
       mpg.setDataSource(dsc);
       // 包配置
       PackageConfig pc = new PackageConfig();
       pc.setModuleName(scanner("模块名"));
       pc.setParent("com.example.demo")
               .setMapper("dao")
               .setService("service")
               .setController("controller")
               .setEntity("model")
               .setXml("dao.mapping");
       mpg.setPackageInfo(pc);

       // 策略配置
       StrategyConfig strategy = new StrategyConfig();
       // 表名生成策略(下划线转驼峰命名)
       strategy.setNaming(NamingStrategy.underline_to_camel);
       // 列名生成策略(下划线转驼峰命名)
       strategy.setColumnNaming(NamingStrategy.underline_to_camel);
       // 是否启动Lombok配置
       strategy.setEntityLombokModel(false);
       // 是否启动REST风格配置
       strategy.setRestControllerStyle(true);
       // 自定义实体父类
       strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
       // 自定义controller父类
       //strategy.setSuperControllerClass("pro.nbbt.base.controller.BaseController");
       // 自定义service父接口
       strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService");
       // 自定义service实现类
       strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
       // 自定义mapper接口
       strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
       strategy.setInclude(scanner("表名"));
       //自定义实体，公共字段
       //strategy.setSuperEntityColumns("id");
       mpg.setStrategy(strategy);

       // 执行
       mpg.execute();
   }

}